import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
from printTest import Ui_Form
from PyQt5.QtCore import pyqtSignal, Qt, QThread, QDateTime
import time


class BackendThread(QThread):
	update_date = pyqtSignal(str)

	def run(self):
		while True:
			data = QDateTime.currentDateTime()
			currTime = data.toString("yyyy-MM-dd hh:mm:ss")
			self.update_date.emit(str(currTime))
			time.sleep(1)


class MyMainWindow(QMainWindow, Ui_Form):
	helpSignal = pyqtSignal(str)
	printSignal = pyqtSignal(list)
	previewSignal = pyqtSignal([int, str], [str])

	def __init__(self, parent=None):
		super(MyMainWindow, self).__init__(parent)
		self.setupUi(self)
		self.initUI()

	def initUI(self):
		self.backend = BackendThread()
		self.backend.update_date.connect(self.handleDisplay)
		self.backend.start()
		self.helpSignal.connect(self.showHelpMessage)
		self.printSignal.connect(self.printPaper)
		self.previewSignal[str].connect(self.previewPaper)
		self.previewSignal[int, str].connect(self.previewPaperWithArgs)
		self.printButton.clicked.connect(self.emitPrintSignal)
		self.previewButton.clicked.connect(self.emitPreviewSignal)

	def handleDisplay(self, data):
		self.resultLabel.setText(data)

	def emitPreviewSignal(self):
		if self.previewStatus.isChecked() == True:
			self.previewSignal[int, str].emit(1080, "Full Screen")
		elif self.previewStatus.isChecked() == False:
			self.previewSignal[str].emit("Preview")

	def emitPrintSignal(self):
		pList = []
		pList.append(self.printNumber.value())
		pList.append(self.stylecomboBox.currentText())
		self.printSignal.emit(pList)

	def printPaper(self, list):
		self.resultLabel.setText("打印：" + "份数：" + str(list[0]) + "纸张:" + str(list[1]))

	def previewPaperWithArgs(self, style, text):
		self.resultLabel.setText(str(style) + text)

	def previewPaper(self, text):
		self.resultLabel.setText(text)

	def keyPressEvent(self, event):
		if event.key() == Qt.Key_F1:
			self.helpSignal.emit("help  message")

	def showHelpMessage(self, message):
		self.resultLabel.setText(message)
		self.statusBar().showMessage(message)


if __name__ == "__main__":
	app = QApplication(sys.argv)
	# widget = loadUi('printTest.ui')
	# widget.show()
	win = MyMainWindow()
	win.show()
	sys.exit(app.exec_())
